package com.atguigu.spzx.product.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.atguigu.spzx.model.dto.h5.ProductSkuDto;
import com.atguigu.spzx.model.dto.product.SkuSaleDto;
import com.atguigu.spzx.model.entity.product.Product;
import com.atguigu.spzx.model.entity.product.ProductDetails;
import com.atguigu.spzx.model.entity.product.ProductSku;
import com.atguigu.spzx.model.vo.h5.ProductItemVo;
import com.atguigu.spzx.product.mapper.ProductDetailsMapper;
import com.atguigu.spzx.product.mapper.ProductMapper;
import com.atguigu.spzx.product.mapper.ProductSkuMapper;
import com.atguigu.spzx.product.service.ProductService;
import com.atguigu.spzx.product.service.ProductSkuService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * ClassName: ProductServiceImpl
 * Package: com.atguigu.spzx.product.service.impl
 * Description:
 *
 * @Author 刘强
 * @Create 2024/7/14 23:34
 * @Version 1.0
 */
@Service
public class ProductServiceImpl implements ProductService {
    @Resource
    private ProductSkuMapper productSkuMapper;
    @Resource
    private ProductMapper productMapper;
    @Resource
    private ProductDetailsMapper productDetailsMapper;
    /**
     * 分页带条件查询商品sku
     * @param page
     * @param limit
     * @param productSkuDto
     * @return
     */
    @Override
    public PageInfo<ProductSku> findByPage(Integer page, Integer limit, ProductSkuDto productSkuDto) {
        PageHelper.startPage(page,limit);
        List<ProductSku> productSkuList =  productSkuMapper.findByKeywords(productSkuDto);
        return new PageInfo<>(productSkuList);
    }

    /**
     * 根据skuId查询商品详情
     * @param skuId
     * @return
     */
    @Override
    public ProductItemVo item(Long skuId) {
        // productSku   -> product_sku
        ProductSku productSku = productSkuMapper.findById(skuId);
        Long productId = productSku.getProductId();
        // product  -> product
        Product product = productMapper.findById(productId);
        // sliderUrlList -> product
        List<String> sliderUrlList = Arrays.asList(product.getSliderUrls().split(","));
        // detailsImageUrlList -> product_details
        ProductDetails productDetails = productDetailsMapper.findByProductId(productId);
        List<String> detailsImageUrlList = Arrays.asList(productDetails.getImageUrls().split(","));
        // specValueList -> product
        JSONArray specValueList = JSON.parseArray(product.getSpecValue());
        // skuSpecValueMap  -> product_sku
        HashMap<String, Object> skuSpecValueMap = new HashMap<>();
        List<ProductSku> productSkuList = productSkuMapper.findByProductId(productId);
        for (ProductSku sku : productSkuList) {
            skuSpecValueMap.put(sku.getSkuSpec(),sku.getId());
        }
        // 返回值的封装
        ProductItemVo productItemVo = new ProductItemVo();
        productItemVo.setProductSku(productSku);
        productItemVo.setProduct(product);
        productItemVo.setSliderUrlList(sliderUrlList);
        productItemVo.setDetailsImageUrlList(detailsImageUrlList);
        productItemVo.setSpecValueList(specValueList);
        productItemVo.setSkuSpecValueMap(skuSpecValueMap);
        return productItemVo;
    }

    @Override
    public ProductSku getBySkuId(Long skuId) {
        ProductSku productSku = productSkuMapper.findById(skuId);
        return productSku;
    }

    /**
     *  跟新库存和销量 -> 库存减少，销量增加
     * @param skuSaleDtoList
     */
    @Override
    public void updateStockAndSaleNum(List<SkuSaleDto> skuSaleDtoList) {
        if (!CollectionUtils.isEmpty(skuSaleDtoList)){
            for (SkuSaleDto skuSaleDto : skuSaleDtoList) {
                productSkuMapper.updateStockAndSaleNum(skuSaleDto);
            }
        }
    }
}
